package cc.forgemod.customicon.server;

import cc.forgemod.customicon.common.CustomIcon;
import cc.forgemod.customicon.common.IProxy;
import cc.forgemod.customicon.common.config.ConfigManager;
import cc.forgemod.customicon.common.icon.IconManager;
import cc.forgemod.customicon.common.network.PacketHandler;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;

public class ServerProxy implements IProxy {

    public static ConfigManager mConfig;

    /**
     * 载入配置,注册监听器
     * 
     * @param pEvent
     *            事件
     */
    @Override
    public void preInit(FMLPreInitializationEvent pEvent) {
        ServerProxy.mConfig = new ConfigManager(CustomIcon.mConfigFile);
        this.reloadConfig(null);

        MinecraftForge.EVENT_BUS.register(this);
    }

    @Override
    public void init(FMLInitializationEvent pEvent) {
        // TODO Auto-generated method stub

    }

    @Override
    public void reloadConfig(ICommandSender pSender) {
        ServerProxy.mConfig.reloadConfig(pSender);
    }

    @SubscribeEvent
    public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent pEvent) {
        if (pEvent.player instanceof EntityPlayerMP) {
            PacketHandler.CHANNEL.sendTo(IconManager.getIconHashListPacket(), (EntityPlayerMP)pEvent.player);
        }
    }

}
